<?php
// title, subtitle and type of chart

function chartOptions($title, $subtitle, $type_chart) {
	
    $content= "var chart;
            $(document).ready(function() {
			
                chart = new Highcharts.Chart({
                    chart: {
                        renderTo: 'container',
                        defaultSeriesType: '" . $type_chart . "'
                    },
                    title: { text: '" . $title . "'  },
                    subtitle: {  text: '" . $subtitle . "'}, ";
	return $content;
}

//  quelques options de Pie chart...
function pieOptions($title, $type, $name){
    return "var chart;
$(document).ready(function() {
   chart = new Highcharts.Chart({
      chart: {
         renderTo: 'container',
         plotBackgroundColor: null,
         plotBorderWidth: null,
         plotShadow: false
      },
      title: {
         text: '$title'
      },
      tooltip: {
         formatter: function() {
            return '<b>'+ this.point.name +'</b>: '+ this.y;
         }
      },
      plotOptions: {
         pie: {
            allowPointSelect: true,
            cursor: 'pointer',
            dataLabels: {
               enabled: true,
			   formatter: function() {
                  return this.point.name +'</b>: '+ this.y;
               }
            },
            showInLegend: true
         }
      },
       series: [{
         type: '$type',
         name: '$name',
         data: [";
}

// build pie chart 
function buildPie($tab){
$content="";
	foreach ($tab as $line){
		settype($line[1], "float");
		$content.="['".$line[0]."' ,      ".$line[1]."],";	
	}
	$content.="]}]";
	$content=str_replace(",]}]", "]}]", $content);
	$content.="});                });";				
return $content;	
}

// c'est pour remplir les donnees dans lea categories ( axe de x) ( ceux ne sont pas des dates ).
function tab_to_accolades_with_cotes($tab) {
    $content = "";
    for ($i = 1; $i < sizeof($tab); $i++) {
        $content.="'" . $tab[$i]. "',";
    }
    $content.="],
	 labels: {
            rotation: -45,
            align: 'right'
         }";
    $content = str_replace(",]", "]", $content);
    return $content; //['1','1','2','3']
}

// c'est pour remplir les donnees dans lea categories ( axe de x) ( ceux sont des dates ).
function tab_to_accolades_with_cotes_for_date($tab) {
    $content = "";
    for ($i = 1; $i < sizeof($tab); $i++) {
        $content.="'" . $tab[$i]->format('Y-m-d') . "',";
    }
    $content.="],
	 labels: {
            rotation: -45,
            align: 'right'
         }";
    $content = str_replace(",]", "]", $content);
    return $content; //['1','1','2','3']
}

// c'est pour remplir les donnees dans les datas ( les courbes ).
function tab_to_accolades_without_cotes($tab) { // remplir les donnees dans le data
    $content = "";
    for ($i = 1; $i < sizeof($tab); $i++) {
        $content.="" . $tab[$i] . ",";
    }
    $content.="]},{";
    $content = str_replace(",]", "]", $content);
    return $content . "";
}

// return le nom de la courbe.
function title($title) {
    return "name: '" . $title . "', ";
}

// les options de yAxis
function echoOptions($coord_name, $type_chart) {
    $content="},
			yAxis: {
				min: 0,
				title: {
					text: '" . $coord_name . "'
				},

			},
			legend: {
				 align: 'right',
				 x: -100,
				 verticalAlign: 'top',
				 y: 20,
				 floating: true,
				 backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColorSolid) || 'white',
				 borderColor: '#CCC',
				 borderWidth: 1,
				 shadow: false
			},
			tooltip: {
				 formatter: function() {
					return '<b>'+ this.x +'</b><br/>'+
						this.series.name +': '+ this.y +'<br/>'";
						if($type_chart=="column"){
							$content.="+'Total: '+ this.point.stackTotal";
						}
						else {
							$content.="";
						}
			$content.="	 }	
			},
		 plotOptions: {
					column: {
					pointPadding: 0.2,
					borderWidth: 0
				 }
			 },	";
    return $content;
}

// on la donne une matrice et ca nous sort la graph.
function buildChart($matrice, $coord_name, $tab) {
    //$tab c'est un tableau qui contient le title, sub-title and type of chart.
	$content=chartOptions($tab[0],$tab[1],$tab[2]);
    $content.= "xAxis: {         categories: [";
    $content.=tab_to_accolades_with_cotes($matrice[0]);
    $content.=echoOptions($coord_name, $tab[2]);
    $content.="	 series: [{";
    for ($i = 1; $i < sizeof($matrice); $i++) {
        $content.=title($matrice[$i][0]);
        $content.="data: [";
        $content.=tab_to_accolades_without_cotes($matrice[$i]);
    }
    $content.="}); });";
    $content = str_replace(",{}", "]}", $content);
    return $content;
}
?>